package com.xuzao.developer.controller;

import com.xuzao.developer.annotation.MethodAnnotation;
import com.xuzao.developer.annotation.ModuleAnnotation;
import com.xuzao.developer.base.model.SysUser;
import com.xuzao.developer.enums.RoleTypeEnum;
import com.xuzao.developer.enums.WorkTypeEnum;
import com.xuzao.developer.tools.entity.Pager;
import com.xuzao.developer.utils.BJUIMap;
import com.xuzao.developer.utils.LoginUserInfoUtil;
import com.xuzao.developer.vo.SysUserVO;
import com.xuzao.developer.dto.SysUserDTO;
import com.xuzao.developer.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;

/**
 * @description 
 * @author xuzao
 * @date 2019-08-22 11:37:49
 **/
@ModuleAnnotation(name = "用户")
@Controller
@RequestMapping("/sysUser/")
public class SysUserController{

	@Resource
	private SysUserService sysUserService;

	@RequestMapping("queryList")
	public String queryList(SysUserDTO dto, Pager pager, Model model) {
		List<SysUserVO> sysUserVOS = sysUserService.list(dto,pager);

		model.addAttribute("list",sysUserVOS);
		model.addAttribute("bo",dto);
		model.addAttribute("workList",WorkTypeEnum.getMap(dto.getWorkType()));
		model.addAttribute("userRole", LoginUserInfoUtil.getLoginUser().getRoleId());
		return "/sys_user/sys_user_list";
	}


	@RequestMapping("add")
	public String add(Model model) {
		model.addAttribute("list",WorkTypeEnum.getMap(null));
		model.addAttribute("bo",new SysUser());
		return "/sys_user/sys_user_add";
	}


	@RequestMapping("update")
	public String update(Model model,Long id) {
		SysUserVO bo = sysUserService.get(id);
		model.addAttribute("list",WorkTypeEnum.getMap(bo.getWorkType()));
		model.addAttribute("bo",bo);
		return "/sys_user/sys_user_update";
	}

	@MethodAnnotation(name = "新增")
	@RequestMapping("addSysUser")
	@ResponseBody
	public BJUIMap addSysUser(SysUserDTO dto){
		BJUIMap map = new BJUIMap();
		map.setMessage("操作成功");
		map.setTabid("sysUser");
		map.setCloseCurrent(BJUIMap.CALLBACK_CLOSE);
		map.setStatusCode(BJUIMap.CODE_OK);

		sysUserService.insert(dto);
		return map;
	}

	@RequestMapping("updateSysUser")
	@ResponseBody
	public BJUIMap updateSysUser(SysUserDTO dto){
		BJUIMap map = new BJUIMap();
		map.setMessage("操作成功");
		map.setTabid("sysUser");
		map.setCloseCurrent(BJUIMap.CALLBACK_CLOSE);
		map.setStatusCode(BJUIMap.CODE_OK);

		sysUserService.update(dto);
		return map;
	}

	@RequestMapping("updatePassword")
	public String updatePassword(Model model) {

		return "/sys_user/sys_user_update_password";
	}

	@RequestMapping("savePassword")
	@ResponseBody
	public BJUIMap savePassword(String  passWord){
		BJUIMap map = new BJUIMap();
		map.setMessage("操作成功");
		map.setCloseCurrent(BJUIMap.CALLBACK_CLOSE);
		map.setStatusCode(BJUIMap.CODE_OK);
		SysUser sysUser = LoginUserInfoUtil.getLoginUser();

		SysUserDTO sysUserDTO = new SysUserDTO();
		sysUserDTO.setId(sysUser.getId());
		sysUserDTO.setPassWord(passWord);
		sysUserService.update(sysUserDTO);
		return map;
	}

	@MethodAnnotation(name = "删除")
	@RequestMapping("del")
	@ResponseBody
	public BJUIMap savePassword(Long  id){
		BJUIMap map = new BJUIMap();
		map.setMessage("操作成功");
		map.setStatusCode(BJUIMap.CODE_OK);
		map.setTabid("sysUser");

		sysUserService.del(id);
		return map;
	}
}